package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.command.base.BaseCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.CheckMatchWebSiteIsLoadCommand;
import com.hxalex.simulationhandle.accessbilityService.manger.CommandManger;
import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;
import com.hxalex.simulationhandle.accessbilityService.receiver.search.serivce.ClickWebContentService;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by hx_Alex on 2018/5/26.
 */

public class CheckMatchWebSiteIsLoadReceiver extends BaseReceiver {
    private AccessibilityNodeInfo nodeInfo;
    private boolean isClickOperaMenu = false;
    private boolean isCompleteNowCommand=false;
    private boolean isStartThread=false;
    public CheckMatchWebSiteIsLoadReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="检查匹配网站是否载入命令";
    }

    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }

    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }


    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("CheckMatchWebSiteIsLoad","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
         if(!isStartThread)
            {
                isStartThread=true;
                TimeOutCheckThread thread=new TimeOutCheckThread();
                thread.start();
            }

        if(searchManger.checkMatchWebSiteIsLoaded(nodeInfo))
        {
            ClickWebContentService.isStratTiming=true;
            isCompleteNowCommand=true;
            commandCompleteListener.commandCompleteListener();
        }
    }


    class  TimeOutCheckThread extends Thread
    {
        @Override
        public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                CommandManger commandManger=CommandManger.getManger();
                BaseCommand nowCommand = commandManger.getCommandQueue().peek();
                //当前命令列表是否还是当前命令
                if(nowCommand instanceof CheckMatchWebSiteIsLoadCommand && !isCompleteNowCommand)
                {
                    Log.w("CheckMatchWebSiteIsLoad","匹配网站载入超时，开始一下次执行");
                    EventBus.getDefault().post(SearchManger.OPEN_WAKE_WINWODS);
                    //重新加载搜索命令，退出服务
                    CommandManger manger = CommandManger.getManger();
                    manger.getCommandQueue().clear();
                    searchManger.getContentMatchList().clear();
                    manger.loadBrowserCommandAgain(SearchManger.getContext(),true);
                    manger.loadBrowserCommand(SearchManger.getContext(),true);
                    ClickWebContentService.isExecuteLoop=false;
                }
        }
    }
}
